package com.wangmeng.book.server.action;

import com.wangmeng.book.dao.BaseDaoSupport;
import com.wangmeng.book.entity.BookPartition;
import com.wangmeng.book.server.Command;
import com.wangmeng.book.server.CommandResult;
import com.wangmeng.book.utils.SpringContextManager;
import net.sf.json.JSONArray;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: 王猛
 * Date: 13-9-29
 */
public class PartitionListAction implements IBaseAction {

    public CommandResult execute(Command cmd) {
        CommandResult cr = new CommandResult();
        try {
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            List<Map<String,Object>> partList = jt.queryForList("select code,name,brief,status,pcode from book_partition order by code");
            String json = JSONArray.fromObject(partList).toString();
            cr.setCode(CommandResult.CODE_SUCCESS);
            Map data = new HashMap();
            data.put("data", json);
            cr.setData(data);
        } catch (Exception ex) {
            cr.setCode(CommandResult.CODE_COMMON_ERROR);
            cr.setMessage(ex.getMessage());
        }
        return cr;
    }
}
